<?php
/**
 * banner
 * @author chenlisong E-mail:chenlisong1021@163.com 
 * @version 创建时间：2015年12月29日 下午3:17:16 
 * 类说明
 */
class BannerAction extends BaseAction{
	/**
	 * @authority 浏览
	 */
	public function index(){
		$Model = M($this->getActionName());
		$map['is_on'] = 1;
		$map['flag'] = 1;
		$vo = $Model->where($map)->order('seq asc')->select();
		$this->assign('vo',$vo);
		$this->display('index');
	}
	
	//列表
	public function getAllData(){
		$Model = M($this->getActionName());
		$map = $this->loadSeachCondition();
		$map['is_on'] = 2;
		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	public function activity(){
		$type = $_REQUEST['type'];
		$this->assign("type",$type);
		$this->display();
	}
	
	//活动会场列表
	public function getMeetingplaceAllData(){
		$type = $_REQUEST['type'];
		$Model = M("Meetingplace");
		$map = $this->loadSeachCondition();
		if($type == 3){
			$map['type'] = array('not in','2');
		}else if($type == 5){
			$map['type'] = 2;
		}
		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		/*
		if(!empty($_GET['field'])){
			$map['field'] = array("like","%".$_GET['field']."%");
		}
		*/
		return $map;
	}
	
	public function product(){
		$Model = M($this->getActionName());
		$map[$Model->getPk()] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$TypeModel = M("Type");
		$condition['reserve'] = 1;
		$condition['flag'] = 1;
		$types = $TypeModel->where($condition)->field("id,type_name as name")->order("seq asc")->select();
		$this->assign("types",$types);
		$this->assign("vo",$vo);
		$this->display();
	}
	
	//产品列表
	public function getProductAllData(){
		$Model = M("Product");
		$map = $this->loadProductSeachCondition();
		$map['a.flag'] = 1;
		$where['flag'] = 1;
		if(!empty($map['type_id'])){
			$where['type_id'] = $map['type_id'];
		}
		$total = $Model->where($where)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		if(!empty($page->sort)){
			$sort = 'a.'.$page->sort;
		}else{
			$sort = $page->sort;
		}
		$data = $Model->alias('a')->join('left join __TYPE__ as b on b.id = a.type_id')->field('a.id,a.name,a.spec,a.desc,a.price,a.is_on,a.seq,a.create_time,b.type_name')->where($map)->limit($page->firstRow,$page->listRows)->order($sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadProductSeachCondition(){
		$map = array();
		if(!empty($_GET['name'])){
			$map['name'] = array("like","%".$_GET['name']."%");
		}
		if(!empty($_GET['type_id'])){
			$map['type_id'] = $_GET['type_id'];
		}
		return $map;
	}
	
	
	
	/**
	 * @authority 修改
	 */
	public function edit() {
		$Model = M($this->getActionName());
		$map[$Model->getPk()] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		if($vo['type'] == 3 || $vo['type'] == 5){
			$MeetingplaceModel = M("Meetingplace");
			$where['id'] = $vo['url'];
			$meetingplaceName = $MeetingplaceModel->where($where)->getField("title");
			$vo['name'] = $meetingplaceName;
		}else if($vo['type'] == 6){
			$ProductModel = M("Product");
			$where['id'] = $vo['url'];
			$name = $ProductModel->where($where)->getField("name");
			$vo['name'] = $name;
		}
		$this->assign('vo',$vo);
		$this->display();
	}
	
	//插入
	public function insert(){
		$Model = M($this->getActionName());
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		if($data['type'] == 5){
			$where['flag'] = 1;
			$where['type'] = 5;
			$count = $Model->where($where)->count();
			if($count != 0){
				$this->ajaxReturn(0,"已有活动专场焦点图，请删除或修改后操作 ",0);
			}
		}
		//查询最大排序
		$map['flag'] = 1;
		$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
		$data['seq'] = $max_seq + 1;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	//更新
	public function update() {
		$Model = D($this->getActionName());	
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		if($data['type'] == 5){
			$where['flag'] = 1;
			$where['type'] = 5;
			$where['id'] = array("not in",$data['id']);
			$count = $Model->where($where)->count();
			if($count != 0){
				$this->ajaxReturn(0,"已有活动专场焦点图，请删除或修改后操作 ",0);
			}
		}
		// 更新数据
		if(false !== $Model->save()) {
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	//修改排序
	public function updateSeq(){
		$Model = M($this->getActionName());
		//批量更新
		$seq = $_REQUEST['seq'];
		$sql = 'SET seq = CASE id ';
		$ids = "";
		foreach ($seq as $key=>$val){
			$ids .= $val.",";
			$key++;
			$sql .= ' WHEN '.$val.' THEN '.$key." ";
		}
		$sql .= ' END ';
		$ids = substr($ids,0,strlen($ids)-1);
		$sql .= 'where id in ('.$ids.')';
		$where['id'] = array('in',$ids);
		$data['_query'] = $sql;
		$result = $Model->execute('update __TABLE__ '.$sql);
		if($result !== false){
			$this->ajaxReturn($Model->getLastSql(),L('ADMIN_SEQ_SUCCESS'),1);
		}else{
			$this->ajaxReturn($Model->getLastSql(),L('ADMIN_SEQ_ERROR'),0);
		}
	}
	
	//停用&启用
	public function isEnabled() {
		$Model = D($this->getActionName());
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		if($data['is_on'] == 1){
			$map['flag'] = 1;
			$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
			$data['seq'] = $max_seq + 1;
		}else if($data['is_on'] == 2){
			$data["seq"] = 0;
		}
		// 更新数据
		if(false !== $Model->save($data)) {
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	//删除
	public function del(){
		$Model = M($this->getActionName());
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$condition = array($pk => array('in', explode(',', $id)));
				if (false !== $Model->where($condition)->setField('flag',2)) {
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}
	
	//图片传图
	public function uploadImg(){
		import("COM.Core.UploadUtils");
		$upload = new UploadUtils();
		$upload->upload('./Uploads/Banner', 'jpg,jpeg,png');
	}

}